package com.hbsx.why.utils.refresh;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

import com.hbsx.why.utils.refresh.listener.ScrollViewListener;

/**
 * Created by liub on 2017/6/27 18:25.
 */

public class PullScrollView extends ScrollView implements Pullable {
    private boolean pullDownEnable = true; //下拉刷新开关
    private boolean pullUpEnable = true; //上拉刷新开关
    private ScrollViewListener scrollViewListener = null;
    public PullScrollView(Context context) {
        super(context);
    }
    public PullScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PullScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    public boolean canPullDown() {
        if (!pullDownEnable) {
            return false;
        }
        // 判断 scrollView 当前滚动位置在顶部
        if(getScrollY() == 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean canPullUp() {
        if (!pullUpEnable) {
            return false;
        }
        // 判断scrollview 滑动到底部
        if (getChildAt(0).getHeight() - getHeight()
                == getScrollY()) {
            return true;
        }
        return false;
    }
    public boolean isPullDownEnable() {
        return pullDownEnable;
    }

    public void setPullDownEnable(boolean pullDownEnable) {
        this.pullDownEnable = pullDownEnable;
    }

    public boolean isPullUpEnable() {
        return pullUpEnable;
    }

    public void setPullUpEnable(boolean pullUpEnable) {
        this.pullUpEnable = pullUpEnable;
    }
    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        }
    }
}
